package cn.xo68.ide.plugin.common.jobs;

import cn.xo68.core.PropertiesDocument;
import cn.xo68.ide.plugin.common.job.JobCallback;
import cn.xo68.ide.plugin.common.job.JobCallbackAdapter;
import cn.xo68.ide.plugin.common.job.ToolSuiteJob;
import cn.xo68.ide.plugin.common.ui.form.GenerateCodeDialog;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.wy2.generator.GenerateResult;
import com.wy2.generator.GeneratorByJdbcConnection;
import com.wy2.generator.database.entity.DatabaseTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedList;
import java.util.Map;

public class CodeGenerateJob implements ToolSuiteJob {

    private static Logger logger = LoggerFactory.getLogger(CodeGenerateJob.class);

    private final String title;
    private final JobCallback jobCallback;
    private final VirtualFile virtualFile;
    private final PropertiesDocument configDoc;
    private final Map<String,Object> model;
    private final LinkedList<DatabaseTable> tables;
    private final GenerateCodeDialog dialog;

    public CodeGenerateJob(String title,VirtualFile virtualFile, PropertiesDocument configDoc, Map<String, Object> model, LinkedList<DatabaseTable> tables, GenerateCodeDialog dialog) {
        this.title = title;
        this.virtualFile = virtualFile;
        this.jobCallback= new JobCallbackAdapter(this.title);
        this.configDoc = configDoc;
        this.model = model;
        this.tables = tables;
        this.dialog= dialog;
    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public boolean isModal() {
        return true;
    }

    @Override
    public boolean isStartInBackground() {
        return true;
    }

    @Override
    public JobCallback getCallback() {
        return new JobCallbackAdapter(this.title){
            @Override
            public void fail(Throwable e) {
                super.fail(e);
            }
        };
    }

    @Override
    public void execute(ProgressIndicator indicator) throws Exception {
        GeneratorByJdbcConnection generatorByJdbcConnection=new GeneratorByJdbcConnection(configDoc);
        LinkedList<GenerateResult> generateResults = generatorByJdbcConnection.generteByTables(tables, model, true);

        logger.info("代码输出目录：{}",generatorByJdbcConnection.getOutputRootDirectory());
        generateResults.forEach(result->{
            logger.info("{} - {} - {}",result.getMarker(),result.getSuccess(),result.getGenerateFilePath());
        });

        //刷新文件
        this.virtualFile.refresh(false,true);
        logger.info(">> 代码生成成功.....");

//        SwingUtilities.invokeLater(()->{
//            CodeGenerateJob.this.dialog.SetResultInfo(">> 删除生成代码成功.....");
//        });

        ApplicationManager.getApplication().invokeLater(()->{
            Messages.showMessageDialog(
                    "代码生成成功"
                    , "代码生成"
                    , Messages.getInformationIcon());
        });



    }
}
